//
//  THJClassViewController.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/11/6.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJClassViewController.h"
#import "JXSegment.h"
#import "JXPageView.h"
#import "THJProductDetailVC.h"
#import "THJSelectView.h"
#import "THJDataProduceCell.h"
#import <MJRefresh.h>


@interface THJClassViewController ()<THJSelectViewDelegate,JXSegmentDelegate,UITableViewDelegate,UITableViewDataSource>
{
    JXSegment * segment;
    NSInteger selectIndex;
    UITableView * _tableview;
    NSString * catid;
    NSString * areaIndex;
    NSString * areaName;
    NSInteger   pageNo;
}
@property(nonatomic,strong)THJSelectView * selectView;
@property(nonatomic,strong)NSMutableArray * titleArr;
@property(nonatomic,strong)NSMutableArray * titleIdArr;
@property(nonatomic,strong)NSMutableArray * productArr;
@property(nonatomic,strong)NSMutableArray * areaArr;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;

@end

@implementation THJClassViewController
-(NSMutableArray*)titleArr{
    if(!_titleArr){
        _titleArr = [[NSMutableArray alloc] init];
    }
    return _titleArr;
}
-(NSMutableArray*)titleIdArr{
    if(!_titleIdArr){
        _titleIdArr = [[NSMutableArray alloc] init];
    }
    return _titleIdArr;
}
-(NSMutableArray*)productArr{
    if(!_productArr){
        _productArr = [[NSMutableArray alloc] init];
    }
    return _productArr;
}
-(NSMutableArray*)areaArr{
    if(!_areaArr){
        _areaArr = [[NSMutableArray alloc] init];
    }
    return _areaArr;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:YES];
    [self setStateBarColor:[UIColor clearColor]];
}
- (void)viewDidLoad {
    selectIndex = 0;
    pageNo = 1;
    areaIndex = @"1";
    [super viewDidLoad];
    [self setShadow];
    [self setRightNavBtn];
    [self getData];
    [self setProductListView];
    [self getAreaData];
    
    if(KIsiPhoneX){
        self.topSpace.constant = 119;
        [self setXClassNavBarWithTitle:@"分类"];
    }else{
        [self setNavBarWithTitle:@"分类"];
        self.topSpace.constant = 110;
    }
    [self SVHUDShow];
}

-(THJSelectView*)selectView{
    if(!_selectView){
        _selectView = [[THJSelectView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _selectView.dataArr = self.areaArr;
        _selectView.delegate = self;
        _selectView.listKey = @"areaname";
    }
    return _selectView;
}
//获取滚动顶部菜单
-(void)getData{
    NSString * url = [NSString stringWithFormat:@"%@%@%@&catid=%@",THJUrlDomain,THJindexPHP,@"f=Category&r=Category&m=three",self.productCatid];
    __weak typeof(self) weakSelf = self;
    [THJNetWork GetDataWithGetRequestTypeUrl:url Callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"rece-%@",receiveData);
        if([receiveData[@"status"] isEqual:@"200"]){
            for (NSDictionary * dic in receiveData[@"data"]) {
                [weakSelf.titleArr addObject:dic[@"catname"]];
                [weakSelf.titleIdArr addObject:dic[@"catid"]];
            }
            catid = weakSelf.titleIdArr[0];
            [weakSelf setTitleMenuWithTitleArr:self.titleArr];
            [weakSelf getDataWithCatId:catid Page:pageNo AreaId:@""];
        }
       
        
    }];
}
//获取产品数据
-(void)getDataWithCatId:(NSString*)catid Page:(NSInteger)page AreaId:(NSString*)areaid{
    NSString * url = [NSString stringWithFormat:@"%@%@%@&catid=%@&page=%@&area=%@",THJUrlDomain,THJindexPHP,@"f=sell_5&r=sell_5",catid,[NSString stringWithFormat:@"%ld",page],areaid];
    NSLog(@"获取产品url-%@",url);
    __weak typeof(self) weakSelf = self;
    [THJNetWork GetDataWithGetRequestTypeUrl:url Callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"rece-%@",receiveData);
        [weakSelf SVHUDHiden];
        [_tableview.mj_footer endRefreshing];
        if(page == 1){
            [weakSelf.productArr removeAllObjects];
            for (NSDictionary * dic in receiveData[@"data"]) {
                [weakSelf.productArr addObject:dic];
            }
            if(!weakSelf.productArr.count){
                [SVProgressHUD showErrorWithStatus:@"暂无相关产品"];
                [_tableview.mj_footer endRefreshingWithNoMoreData];
            }
        }else{
            NSArray *arr = receiveData[@"data"];
            if(!arr.count){
                [_tableview.mj_footer endRefreshingWithNoMoreData];
            }
            for (NSDictionary * dic in receiveData[@"data"]) {
                [weakSelf.productArr addObject:dic];
            }
        }
        [_tableview reloadData];
    }];
}
//获取地区数据
-(void)getAreaData{
    NSString * url = [NSString stringWithFormat:@"%@%@%@",THJUrlDomain,THJindexPHP,@"f=Area&r=Area"];
    __weak typeof(self) weakSelf = self;
    [THJNetWork GetDataWithGetRequestTypeUrl:url Callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"rece-%@",receiveData);
        weakSelf.areaArr = [receiveData[@"data"] mutableCopy];
        NSDictionary * dic = @{@"areaid":@"0",@"areaname":@"全国"};
        [weakSelf.areaArr insertObject:dic atIndex:0];
    }];
}
//设置顶部菜单栏
-(void)setTitleMenuWithTitleArr:(NSMutableArray*)arr{
    CGFloat constant;
    if(KIsiPhoneX){
        constant = 74;
    }else{
        constant = 64;
    }
    segment = [[JXSegment alloc] initWithFrame:CGRectMake(0, constant, ScreenWidth, 45)];
    segment.scrollView.backgroundColor = COLOR(83, 84, 85, 1);
    [segment updateChannels:arr];
    segment.delegate = self;
    [self.view addSubview:segment];
}
//产品列表
-(void)setProductListView{
    CGFloat constant;
    if(KIsiPhoneX){
        constant = 175;
    }else{
        constant = 165;
    }
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, constant, ScreenWidth, ScreenHeight - constant) style:UITableViewStylePlain];
    [_tableview registerNib:[UINib nibWithNibName:@"THJDataProduceCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _tableview.delegate = self;
    _tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    _tableview.tableFooterView = [UIView new];
    _tableview.rowHeight = 120;
    _tableview.dataSource = self;
    [self.view addSubview:_tableview];
}
//设置阴影
-(void)setShadow{
    self.btnBgView.layer.shadowColor = [UIColor lightGrayColor].CGColor;//设置阴影的颜色
    self.btnBgView.layer.shadowOpacity = 0.8;//设置阴影的透明度
    self.btnBgView.layer.shadowOffset = CGSizeMake(5, 5);//设置阴影的偏移量
    self.btnBgView.layer.shadowRadius = 3;//设置阴影的圆角
}
//下拉加载
-(void)loadMore{
    NSLog(@"bnsl");
    pageNo++;
    [self getDataWithCatId:catid Page:pageNo AreaId:areaIndex];
}
#pragma mark - tableviewDelegate --
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.productArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dic = self.productArr[indexPath.row];
    THJDataProduceCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell setCellContentWithDic:dic];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = self.productArr[indexPath.row];
    THJProductDetailVC * vc = [THJProductDetailVC new];
    vc.itemid = dic[@"itemid"];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - THJCompanyProductViewDelegate
-(void)cellClickedIndex:(NSInteger)index{
    NSLog(@"index--%ld",index);
    [self.navigationController pushViewController:[THJProductDetailVC new] animated:YES];
}
#pragma mark - JXPageViewDataSource
-(NSInteger)numberOfItemInJXPageView:(JXPageView *)pageView{
    return self.titleArr.count;
}
-(UIView*)pageView:(JXPageView *)pageView viewAtIndex:(NSInteger)index{
    UIView *view = [[UIView alloc] init];
    return view;
}
#pragma mark - JXSegmentDelegate
-(void)JXSegment:(JXSegment*)segment didSelectIndex:(NSInteger)index{
    NSLog(@"index--%ld",index);
    
    selectIndex = index;
    catid = self.titleIdArr[index];
    NSLog(@"index--%ld-%@",index,catid);
    [self.productArr removeAllObjects];
    [self getDataWithCatId:catid Page:1 AreaId:areaIndex];
}
#pragma mark - JXPageViewDelegate -
- (void)didScrollToIndex:(NSInteger)index{
    [segment didChengeToIndex:index];
    NSLog(@"**-%ld",(long)index);
}
#pragma mark - THJSelectViewDelegate --
-(void)selectIndex:(NSInteger)index{
    NSLog(@"area-index-%ld",index);
    if(index != 999999){
        [self.productArr removeAllObjects];
        NSDictionary * dic = self.areaArr[index];
        [self.btn2 setTitle:dic[@"areaname"] forState:UIControlStateNormal];
        areaIndex = dic[@"areaname"];
        [self getDataWithCatId:catid Page:1 AreaId:areaIndex];
    }
}
//综合排序
- (IBAction)btn1Click:(id)sender {
    NSLog(@"综合排序");
}
//地区
- (IBAction)btn2Click:(id)sender {
    NSLog(@"地区");
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
//    [self setStateBarColor:SingleColor(clearColor)];
    [window addSubview:self.selectView];
}
//返回按钮
- (IBAction)backBtnAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
